﻿using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace class11Homework
{
    public partial class Form1 : Form
    {
        static string connstr = "server=localhost;Database =class11homework;uid=root;pwd=ljt2002818;charset=utf8";
        MySqlConnection conn = new MySqlConnection(connstr);
        static int IdOfWords = 1;
        string sql = null;
        string input = null;
        string answer = null;
    public Form1()
        {
            InitializeComponent();
            try
            {
                sql = "SELECT ChineseWord FROM words WHere id=" + IdOfWords;
                MySqlCommand cmd = new MySqlCommand(sql, conn);
                conn.Open();
                MySqlDataReader dataReader = cmd.ExecuteReader();
                if (dataReader.Read())
                {
                    label1.Text = (string)dataReader["ChineseWord"];
                }
                dataReader.Close();
            }
            catch (Exception)
            {

            }
            finally
            {
                conn.Close();
            }

        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            input = textBox1.Text;
            try
            {
                sql = "SELECT EnglishWord FROM words WHere id=" + IdOfWords;
                MySqlCommand cmd = new MySqlCommand(sql, conn);
                conn.Open();
                MySqlDataReader dataReader = cmd.ExecuteReader();
                if (dataReader.Read())
                {
                    answer = dataReader["EnglishWord"].ToString();
                }
                dataReader.Close();
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                conn.Close();
            }
            if(input.Equals(answer))
            {
                label3.Text = "回答正确";
            }
            else
            {
                label3.Text = "回答错误";
            }

        }

        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                sql = "SELECT EnglishWord FROM words WHere id=" + IdOfWords;
                MySqlCommand cmd = new MySqlCommand(sql, conn);
                conn.Open();
                MySqlDataReader dataReader = cmd.ExecuteReader();
                if (dataReader.Read())
                {
                    answer = dataReader["EnglishWord"].ToString();
                }
                dataReader.Close();
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                conn.Close();
            }
            label4.Text = answer;
        }

        private void button3_Click(object sender, EventArgs e)
        {
            IdOfWords++;
            try
            {
                sql = "SELECT ChineseWord FROM words WHere id=" + IdOfWords;
                MySqlCommand cmd = new MySqlCommand(sql, conn);
                conn.Open();
                MySqlDataReader dataReader = cmd.ExecuteReader();
                if (dataReader.Read())
                {
                    label1.Text = dataReader["ChineseWord"].ToString();
                }
                label4.Text = "";
                textBox1.Text = "";
                label3.Text = "";
                dataReader.Close();
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                conn.Close();
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            
        }
    }
}
